// use/work/work.js
var app = getApp();
var myInterval;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    status:1,
    imagePath:"",
    tip:"",
    product_iot_id:"",
    device_id:"",
    last_use_time:0,
    last_time:"",
    progress: false,
    percent: 0,
    ordernumber:"",
    setI:"",
    o_id:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    var ids;
    var that = this;
    var status = this.data.status;
    var ordernumber = "";
    if (options.ordernumber) {
      ordernumber = options.ordernumber;
    }
    // var ordernumber = options.ordernumber;
    var last_use_time = "";
    if (options.last_use_time) {
      if (isNaN(new Date(options.last_use_time).getTime())) {
        last_use_time = 330 - (Math.floor(new Date().getTime() / 1000) - Number(options.last_use_time));
      } else {
        last_use_time = 330 - Math.floor(new Date().getTime() / 1000) + (new Date(options.last_use_time).getTime() / 1000);
      }
    } else {
      last_use_time = app.globalData.useTime;
    }
    if (last_use_time <= 0) {
      last_use_time = app.globalData.useTime;
    }
    var hour = "00";
    var minute = Math.floor(last_use_time / 60);
    var second = last_use_time - minute * 60 ;
    minute = minute > 10 ? minute : "0" + minute;
    second = second > 10 ? second : "0" + second;
    var last_time = hour + ":" + minute + ":" + second;
    this.setData({
      last_use_time: last_use_time,
      last_time: last_time
    })
    if (options.ids) {
      ids = JSON.parse(options.ids);
      var device_id = ids.device_id;
      var product_iot_id = ids.product_iot_id;
      var o_id = "";
      if (ids.id) {
        o_id = ids.id;
      }
      that.setData({
        device_id: device_id,
        product_iot_id: product_iot_id,
        status: status ? status : 1,
        o_id: o_id,
        ordernumber: ids.ordernumber || ""
      })
    }
    if (options.qrcode) {
      var qrcode = options.qrcode;
      app.getRequest("api/device/qrcodeDevice", { device_qrcode_id: qrcode},function(res){
        if (res.code === 200) {
          var status = res.data.device_status
          // if (status === 1 && res.data.has_rolling != 0 ){
          //   wx.showModal({
          //     title: '操作',
          //     content: '请确定打开卷帘门',
          //     confirmText: '确定',
          //     showCancel: true,
          //     success: (e) => { 
          //       console.log(e);
          //       if (e.confirm){
          //         that.openDoor();
          //       }else{
          //         wx.navigateBack()
          //       }
          //     }
          //   })
          // }
          if (status === 1) {
            that.setData({
              imagePath: "/images/start.png",
              tip: "请将车辆停入准确位置！",
              status: status
            })
          } else {
            that.setData({
              imagePath: "/images/stop.png",
              tip: "设备运行中，请勿下车",
              status: status
            })
            that.setTime();
          }
        } else {
          wx.showToast({
            title: res.message,
            icon: "none"
          })
        }
      })
    }
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // clearInterval(this.interval);
    app.getUserInfoFun();
    var that = this;
    // setInterval(function(){
    //   var num = that.data.percent;
    //   num = num + 2;
    //   that.setData({
    //     percent: num
    //   })
    // },200)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    // clearInterval(this.interval);
    // clearInterval(myInterval);
    // clearInterval(this.setI);
    // wx.removeStorageSync('code');
    this.setData({
      percent: 0
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    // clearInterval(this.interval);
    clearInterval(myInterval);
    clearInterval(this.setI);
    wx.removeStorageSync('code');
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  /**
   * 暂停或者重启
   */
  changeStatus: function() {
    var status = this.data.status;
    var state = "";
    var tip = "";
    var that = this;
    var imagePath = this.data.imagePath;
    var num = 0;
    that.setData({
      percent: 0
    })
    this.setI = setInterval(function () {
      num = num + 2;
      if (num >= 95) {
        clearInterval(that.setI);
      } else {
        that.setData({
          percent: num
        })
      }
    }, 200);
    if(status == 2){
      var params = {
        product_iot_id: this.data.product_iot_id,
        device_id: this.data.device_id,
        o_id: this.data.o_id,
        operate: "end"
      }
      that.setData({
        progress: true,
        percent: 0
      })
      
      app.getRequest("api/device/playDevice", params, function (res) {
        if(res.code == 200) {
          app.getRequestSync("api/device/mnstest", { msgid: res.data.msgId}, function (req) {
            clearInterval(that.setI);
            if (req.code == 200 && req.data.data.success!="false") {
              // clearInterval(that.interval)
              // status = 1;
              // state = "洗车暂停";
              // imagePath = "/images/start.png";
              // tip = "设备已停止"
              // wx.setNavigationBarTitle({
              //   title: state
              // })
              // that.setData({
              //   status: status,
              //   tip: tip,
              //   imagePath: imagePath
              // })
              // that.setData({
              //   percent: 100
              // })
              that.setData({
                progress: false,
                percent: 100
              })
              clearInterval(myInterval);
              wx.removeStorageSync('code');
              // 汽车停止订单完成
              wx.redirectTo({
                url: '/use/complate/complate',
              })
            } else {
              that.setData({
                percent: 100,
                progress: false
              })
              if (req.data.data.info === "carNotInPlace"){
                // wx.showToast({
                //   title: "车辆未停入指定位置！",
                //   icon: "none"
                // })
                wx.showModal({
                  title: '提醒',
                  content: '车辆未停入指定位置！',
                })
              } else {
                wx.showToast({
                  title: "发生未知错误",
                  icon: "none"
                })
              }
            }
          })
          
        } else {
          that.setData({
            percent: 100,
            progress: false
          })
          wx.showToast({
            title: res.message,
            icon: "none"
          })
        }
      })
    } else {
      var params = {
        product_iot_id: this.data.product_iot_id,
        device_id: this.data.device_id,
        o_id: this.data.o_id,        
        operate: "start"
      }
      that.setData({
        progress: true
      })
      app.getRequest("api/device/playDevice", params, function (res) {
        if (res.code == 200) {
          app.getRequestSync("api/device/mnstest", { msgid: res.data.msgId }, function (req) {
            clearInterval(that.setI);
            console.log(req.data)
            if (req.code == 200 && req.data.data.success!="false") {
              status = 2;
              state = "洗车进行中";
              imagePath = "/images/stop.png";
              tip = "设备运行中请勿下车";
              wx.setNavigationBarTitle({
                title: state
              })
              that.setData({
                status: status,
                tip: tip,
                imagePath: imagePath,
                last_use_time: 330
              })
              that.setTime();
              that.setData({
                percent: 100,
                progress: false
              })
            } else {
              that.setData({
                percent: 100,
                progress: false
              })
              if (req.data.data.info === "carNotInPlace") {
                // wx.showToast({
                //   title: "车辆未停入指定位置！",
                //   icon: "none"
                // })
                wx.showModal({
                  title: '提醒',
                  content: '车辆未停入指定位置！',
                })
              } else {
                wx.showToast({
                  title: "发生未知错误",
                  icon: "none"
                })
              }
              
            }
          })
        } else {
          that.setData({
            percent: 100,
            progress: false
          })
          wx.showToast({
            title: res.message,
            icon: "none"
          })
        }
      })
    }
   
  },
  /**
   * 完成
   */
  complate: function(){
    wx.redirectTo({
      url: '/use/complate/complate',
    })
  },
  /**
   * 操作设备
   */
  setDepart: function(status) {
    var params = {
      product_iot_id: this.data.product_iot_id,
      device_id: this.data.device_id,
      o_id: this.data.o_id,      
      operate: status
    }
    app.getRequest("api/device/playDevice",params,function(res){
      console.log(res)
    })
  },
  setTime: function () {
    var params = {
      id: this.data.o_id, 
    }
    myInterval = setInterval(function () {
      app.getRequest("api/Order/getOrderInfo", params, function (res) {
        console.log(res, "topqiang")
        if (res.code == 200 && (res.data.status == 4)) {
          clearInterval(myInterval);
          wx.removeStorageSync('code');
          wx.redirectTo({
            url: '/use/complate/complate',
          })
        }
      },"post",false)
    }, 5000)
  }
  // 倒计时
  // setTime: function() {
  //   var that = this;
  //   this.interval = setInterval(function () {
  //     var last_use_time = that.data.last_use_time;
  //     var hour = Math.floor(last_use_time/60/60) || 0;
  //     var minute = Math.floor((last_use_time - hour * 60 * 60) / 60) || 0;
  //     var second = last_use_time - (hour * 60 * 60) - (minute* 60);
  //     hour = (hour > 9 || hour < 0) ? hour: "0" + hour;
  //     minute = minute > 9 ? minute : "0" + minute;
  //     second = second > 9 ? second : "0" + second; 
  //     var last_time = hour + ":" + minute + ":" + second;
  //     that.setData({
  //       last_use_time: last_use_time - 1,
  //       last_time: last_time
  //     })
  //     if (last_use_time <= 0) {
  //       // console.log(that.data.ordernumber);
  //       clearInterval(that.interval);
  //       app.getRequest("api/order/detailsUserOrder", { ordernumber: that.data.ordernumber},function(res) {
  //         if(res.data.status == "4") {
  //           wx.redirectTo({
  //             url: '/use/complate/complate',
  //           })
  //           that.setData({
  //             last_use_time: 300
  //           })
  //         } else {
  //           wx.redirectTo({
  //             url: '/use/complate/complate',
  //           })
  //           that.setData({
  //             last_use_time: 1
  //           })
  //         }
  //       })
  //       // this.changeStatus()
  //     }
  //   }, 1000)
  // }
})